package dichotomy.search.range;

/**
 * @date 2020/06/14
 * @author 王光浩
 * @thinking 使用二分法的模板二和模板三
 */
public class MyMethodThree {

	public int[] searchRange(int[] nums, int target) {
		int[] ret = { -1, -1 };
		if (nums == null || nums.length == 0)
			return ret;
		int left = 0;
		int right;
		int mid;
		// 查找左边界
		right = nums.length;
		while (left < right) {
			mid = left + (right - left) / 2;
			if (nums[mid] >= target)
				right = mid;
			else
				left = mid + 1;
		}
		if (left == nums.length || nums[left] != target)
			return ret;
		ret[0] = left;
		// 查找右边界
		right = nums.length;
		while (left < right) {
			mid = left + (right - left) / 2;
			if (nums[mid] <= target)
				left = mid + 1;
			else
				right = mid;
		}
		if (nums[left - 1] == target)
			ret[1] = left -1;
		return ret;
	}
}
